﻿using IShoppingBll;
using IShoppingDal;
using Microsoft.EntityFrameworkCore;
using ShoppingEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShoppingBll
{
    public class Top_upBll : BaseDeleteBll<Top_up>, ITop_upBll
    {
        private IUserInfoDal _userInfoDal;
        private ITop_upDal _top_UpDal;

        private ShoppingDbContext _dbContext;

        public Top_upBll(ITop_upDal top_UpDal, IUserInfoDal userInfoDal, ShoppingDbContext dbContext) : base(top_UpDal)
        {
            _userInfoDal = userInfoDal;
            _top_UpDal = top_UpDal;
            _dbContext = dbContext;

        }

        /// <summary>
        /// 添加充值记录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool AddTop_Up(Top_up entity)
        {

            using (var transaction = _dbContext.Database.BeginTransaction())
            {

                //获取用户信息
                var userInfo = _userInfoDal.GetEntity(entity.UserId);
                if (userInfo != null && entity != null)
                {
                    //添加余额
                    bool isSuccess = _top_UpDal.Add(entity);
                    //修改用户的余额
                    userInfo.Balance += entity.Amount;
                    bool isSuccess2 = _userInfoDal.Update(userInfo);
                    if (isSuccess && isSuccess2)
                    {
                        transaction.Commit();
                        return true;
                    }
                    else
                    {
                        transaction.Rollback();
                        return false;
                    }
                }
                else
                {
                    transaction.Rollback();
                    return false;
                }
            }
        }


        //充值金额记录列表显示
        public object GetTop_upListByPage(int page, int limit, out int count, string userId)
        {
            //获取数据库中金额表未删除的数据（为真正查询）
            var top_ups = _top_UpDal.GetEntitiesDb().Where(m => m.IsDelete == false);

            if (!string.IsNullOrEmpty(userId))
            {
                top_ups = top_ups.Where(u => u.UserId.Contains(userId));
            }

            //查询出来数据的数量
            count = top_ups.Count();

            var list = (from b in top_ups
                        join sq in _userInfoDal.GetEntitiesDb().Where(s => !s.IsDelete)
                        on b.UserId equals sq.ID into tempCqC
                        from dd in tempCqC.DefaultIfEmpty()

                        select new
                        {
                            b.ID,
                            b.CreateTime,
                            b.Amount,
                            b.Way,
                            dd.UserName,
                        }); 

            //分页
            list = list.OrderBy(u => u.CreateTime).Skip((page - 1) * limit).Take(limit);
            var ddlist = list.ToList().Select(m => new
            {
                m.ID,
                m.Way,
                m.Amount,
                m.UserName,
                CreteTime = m.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")

            });

            return ddlist;

        }
    }
}
